package cn.nawang.ebeim.server.task;

import cn.nawang.ebeim.server.constants.Config;
import cn.nawang.ebeim.server.constants.Constant;
import cn.nawang.ebeim.server.entity.*;
import cn.nawang.ebeim.server.util.ApiVisitUtil;

import java.io.File;
import java.util.UUID;

/**
 * Created by GanJc on 2015-11-13 14:31
 */
public class UpdateToApiServerTask implements IOTask {

    private static StorageCloud storageCloud = null;

    private String fileType ;

    private File dsDir ;

    public UpdateToApiServerTask(String fileType, File dsDir) {
        this.fileType = fileType;
        this.dsDir = dsDir;
    }

    @Override
    public void execute() {
        updateToApiServer(fileType,dsDir);
    }

    private void updateToApiServer(String fileType ,File dsDir) {

        if(storageCloud == null){
            storageCloud = ApiVisitUtil.getStorageCloudByName(Config.STORAGE_CLOUD_NAME);
        }

        // 往storage_file_todo表写入记录
        if (fileType.equals(Constant.FILE_TYPE_FILE)) {
            DataSource ds = ApiVisitUtil.getDataSource(dsDir.getParentFile().getName());
            String fileName=dsDir.getName();
            String trueFileName = fileName.substring(fileName.lastIndexOf('_')+1, fileName.length());
            StorageFileToDo sftd = ApiVisitUtil.getStorageFileToDoByDsIdAndSignature(
                    dsDir.getParentFile().getName(), trueFileName);
            // 判断数据源对象和StorageFileToDo是否存在
            if (ds != null && sftd == null) {
                sftd = new StorageFileToDo();
                sftd.setId(UUID.randomUUID().toString());
                sftd.setCopyCount(1l);
                sftd.setDatasourceId(dsDir.getParentFile().getName());
                sftd.setSignature(trueFileName);
                sftd.setStatus(Constant.STORAGE_FILE_STATUS_ZERO);
                sftd.setStorageCloudId1(storageCloud.getId());
                ApiVisitUtil.addStorageFileToDo(sftd);
            }
            // 三个存储云都满了，才进行新增、更新、删除
            else if (ds != null && sftd != null && sftd.getStorageCloudId1() != null
                    && !"".equals(sftd.getStorageCloudId1()) && sftd.getStorageCloudId2() != null
                    && !"".equals(sftd.getStorageCloudId2()) && sftd.getStorageCloudId3() != null
                    && !"".equals(sftd.getStorageCloudId3())) {
                addStorageFile(sftd);
                updateDataSource(sftd);
                ApiVisitUtil.deleteStorageFileToDo(sftd.getId());
            }
            // 三个存储云未满的话只做更新操作
            else if (ds != null && sftd != null) {
                sftd.setStatus(Constant.STORAGE_FILE_STATUS_ZERO);
                long copyCount = sftd.getCopyCount() + 1;
                sftd.setCopyCount(copyCount);
                // 记录已迁移的存储云
                if (sftd.getStorageCloudId2() == null || "".equals(sftd.getStorageCloudId2())) {
                    sftd.setStorageCloudId2(storageCloud.getId());
                } else {
                    sftd.setStorageCloudId3(storageCloud.getId());
                }
                ApiVisitUtil.updataStorageFileToDo(sftd);
            }
        }


        // 往storage_version_tree_todo表写入记录
        if (fileType.equals(Constant.FILE_TYPE_VERSION)) {
            DataSource ds = ApiVisitUtil.getDataSource(dsDir.getParentFile().getName());
            String fileName=dsDir.getName();
            String trueFileName = fileName.substring(fileName.lastIndexOf('_')+1, fileName.length());
            StorageVersionTreeToDo svttd = ApiVisitUtil.getStorageVersionTreeToDoByDsIdAndVersion(
                    dsDir.getParentFile().getName(), trueFileName);
            // 判断数据源对象和StorageVersionTreeToDo是否存在
            if (ds != null && svttd == null) {
                svttd = new StorageVersionTreeToDo();
                svttd.setCopyCount(1l);
                svttd.setDatasourceId(dsDir.getParentFile().getName());
                svttd.setId(UUID.randomUUID().toString());
                svttd.setVersion(trueFileName);
                svttd.setStatus(Constant.STORAGE_FILE_STATUS_ZERO);
                svttd.setStorageCloudId1(storageCloud.getId());
                ApiVisitUtil.addStorageVersionTreeToDo(svttd);
            }
            // 三个存储云都满了，才进行新增、更新、删除
            else if (ds != null && svttd != null && svttd.getStorageCloudId1() != null
                    && !"".equals(svttd.getStorageCloudId1()) && svttd.getStorageCloudId2() != null
                    && !"".equals(svttd.getStorageCloudId2()) && svttd.getStorageCloudId3() != null
                    && !"".equals(svttd.getStorageCloudId3())) {
                addStorageVersionTree(svttd);
                updateDataSource(svttd);
                ApiVisitUtil.deleteStorageVersionTreeToDo(svttd.getId());
            }
            // 三个存储云未满的话只做更新操作
            else if (ds != null && svttd != null) {
                svttd.setStatus(Constant.STORAGE_FILE_STATUS_ZERO);
                long copyCount = svttd.getCopyCount() + 1;
                svttd.setCopyCount(copyCount);
                // 记录已迁移的存储云
                if (svttd.getStorageCloudId2() == null || "".equals(svttd.getStorageCloudId2())) {
                    svttd.setStorageCloudId2(storageCloud.getId());
                } else {
                    svttd.setStorageCloudId3(storageCloud.getId());
                }
                ApiVisitUtil.updataStorageVersionTreeToDo(svttd);
            }
        }
    }

    /**
     * 将数据添加到已处理列表
     */
    public void addStorageFile(StorageFileToDo obj) {
        StorageFile sf = new StorageFile();
        sf.setId(UUID.randomUUID().toString());
        sf.setCopyCount(obj.getCopyCount());
        sf.setDatasourceId(obj.getDatasourceId());
        sf.setSignature(obj.getSignature());
        sf.setStorageCloudId1(obj.getStorageCloudId1());
        sf.setStorageCloudId2(obj.getStorageCloudId2());
        sf.setStorageCloudId3(obj.getStorageCloudId3());
        ApiVisitUtil.addStorageFile(sf);
    }

    /**
     * 更新数据源备份数量
     */
    public void updateDataSource(StorageFileToDo obj) {
        DataSource ds=ApiVisitUtil.getDataSource(obj.getDatasourceId());
        ds.setCount(obj.getCopyCount().intValue());
        ApiVisitUtil.updataDatasource(ds);
    }

    /**
     * 将数据添加到已处理列表
     */
    public void addStorageVersionTree(StorageVersionTreeToDo obj) {
        StorageVersionTree sf = new StorageVersionTree();
        sf.setId(UUID.randomUUID().toString());
        sf.setCopyCount(obj.getCopyCount());
        sf.setVersion(obj.getVersion());
        sf.setDatasourceId(obj.getDatasourceId());
        sf.setStorageCloudId1(obj.getStorageCloudId1());
        sf.setStorageCloudId2(obj.getStorageCloudId2());
        sf.setStorageCloudId3(obj.getStorageCloudId3());
        ApiVisitUtil.addStorageVersionTree(sf);
    }

    /**
     * 更新数据源备份数量
     */
    public void updateDataSource(StorageVersionTreeToDo obj) {
        DataSource ds=ApiVisitUtil.getDataSource(obj.getDatasourceId());
        ds.setCount(obj.getCopyCount().intValue());
        ApiVisitUtil.updataDatasource(ds);
    }

}
